/**
 * Created by niuchuang on 2017/5/26.
 */

import { StackActions, NavigationActions } from "react-navigation";
import { Alert } from "react-native";
import { WXLogin } from "./WeichatUtils";
import StorageUtil from "./StorageUtil";
import EnumConstants from "../common/EnumConstants";

const {
  DeviceListType, H5Type, EditType, OrderType,
} = EnumConstants;

let topNavigator = null;
// 添加点击判断
let debounce = true;
const NavigatorUtil = {
  /**
   *
   * @param props
   * @param key
   * @returns {*}
   */
  getPageParam: (props, key) => {
    // console.log(props);
    let routerParam = props.screenProps.routerParam ? props.screenProps.routerParam : undefined;
    if (routerParam === undefined || !props.navigation.state.key.startsWith("Init")) {
      routerParam = props.navigation.state.params;
    }

    if (routerParam !== undefined && key in routerParam) {
      return routerParam[key];
    }
    return undefined;

    // return JsonUtil.strToJson(routerParam)
  },

  /**
   *
   * @param props
   * @param key
   * @returns {*}
   */
  getParams: (props, key) => {
    // console.log(props);
    let routerParam = props.screenProps.routerParam ? props.screenProps.routerParam : undefined;
    if (routerParam === undefined || !props.navigation.state.key.startsWith("Init")) {
      routerParam = props.navigation.state.params;
    }
    return routerParam;
  },
  /**
   *
   * @param navigation
   */
  goBack: () => {
    const backAction = NavigationActions.back({
      key: null,
    });
    topNavigator.dispatch(backAction);
  },
  setTopLevelNavigator: navigatorRef => {
    topNavigator = navigatorRef;
  },
  doDispatch: action => {
    if (debounce) {
      debounce = false;
      setTimeout(() => {
        debounce = true;
      }, 500);
      topNavigator.dispatch(action);
    }
  },
  goLogin: () => {
    StorageUtil.clearUser();
    const action = NavigationActions.navigate({
      routeName: "Login",
    });
    NavigatorUtil.doDispatch(action);
  },
  goModalLogin: () => {
    StorageUtil.clearUser();
    const action = NavigationActions.navigate({
      routeName: "ModalLogin",
      params: { isModal: true },
    });
    NavigatorUtil.doDispatch(action);
  },
  checkLogin: () => {
    return new Promise((resolve, reject) => {
      if (global.hasLoggedin) {
        resolve();
      } else {
        // NavigatorUtil.goModalLogin();
        WXLogin();
      }
    });
  },
  goRootPage: () => {
    const action = StackActions.reset({
      index: 0,
      actions: [NavigationActions.navigate({ routeName: "Root" })],
    });
    topNavigator.dispatch(action);
  },
  goAppRootPage: () => {
    const action1 = NavigationActions.navigate({
      routeName: "App",
      params: { isModal: false },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goTest: index => {
    const action1 = NavigationActions.navigate({
      routeName: "Test",
      params: { index },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goUpdatePage: (downloadUrl, version) => {
    const action = StackActions.reset({
      index: 0,
      actions: [
        NavigationActions.navigate({
          routeName: "UpToNewVersion",
          params: {
            downloadUrl,
            version,
          },
        }),
      ],
    });
    NavigatorUtil.doDispatch(action);
  },
  switchToUpdatePage: (downloadUrl, version) => {
    const action = NavigationActions.navigate({
      routeName: "UpToNewVersion",
      params: {
        downloadUrl,
        version,
      },
    });
    NavigatorUtil.doDispatch(action);
  },
  goSearch: () => {
    const action1 = NavigationActions.navigate({
      routeName: "Search",
    });
    NavigatorUtil.doDispatch(action1);
  },
  openH5Page: (url, title) => {
    const action1 = NavigationActions.navigate({
      routeName: "H5Page",
      params: {
        url,
        title,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },

  goUpdateInfo: () => {
    const action1 = NavigationActions.navigate({
      routeName: "UpdateInfo",
    });
    NavigatorUtil.doDispatch(action1);
  },
  goChooseTrouble: () => {
    const action1 = NavigationActions.navigate({
      routeName: "ChooseTrouble",
    });
    NavigatorUtil.doDispatch(action1);
  },
  goDeviceModelWithCallBack: callback => {
    const action1 = NavigationActions.navigate({
      routeName: "ChooseDeviceModel",
      params: {
        callback,
        type: DeviceListType.MAINTAIN,
        tag: 0,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goMaintainDeviceModel: serviceId => {
    const action1 = NavigationActions.navigate({
      routeName: "ChooseDeviceModel",
      params: {
        type: DeviceListType.MAINTAIN,
        tag: 0,
        serviceId,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goRecycleDeviceModel: () => {
    const action1 = NavigationActions.navigate({
      routeName: "ChooseDeviceModel",
      params: {
        type: DeviceListType.RECYCLE,
        tag: 0,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goPadRecycleDeviceModel: () => {
    const action1 = NavigationActions.navigate({
      routeName: "ChooseDeviceModel",
      params: {
        type: DeviceListType.RECYCLE,
        tag: 1,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goServiceInfo: serviceItem => {
    const action1 = NavigationActions.navigate({
      routeName: "ServiceInfo",
      params: {
        serviceItem,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goRecyclePriceInfo: serviceItem => {
    const action1 = NavigationActions.navigate({
      routeName: "RecyclePriceInfo",
      params: {
        serviceItem,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goHelpCenter: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "HelpCenter",
        params: {
          title: "帮助中心",
          type: H5Type.HELPCENTER,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goSuggest: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "Suggest",
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goAccessResultInfo: (deviceInfo, accessInfo, recycleValues, callback) => {
    const action1 = NavigationActions.navigate({
      routeName: "AccessResultInfo",
      params: {
        deviceInfo,
        accessInfo,
        recycleValues,
        callback,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goSetting: userInfo => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "Setting",
        params: { userInfo },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goAboutUs: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "HelpCenter",
        params: {
          title: "关于我们",
          type: H5Type.ABOUTUS,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goRecyclePlaceOrder: param => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "PlaceServiceOrder",
        params: {
          param,
          type: OrderType.RECYCLE,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goUpdatePlaceOrder: param => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "PlaceServiceOrder",
        params: {
          param,
          type: OrderType.UPDATE,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goServicePlaceOrder: param => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "PlaceServiceOrder",
        params: {
          param,
          type: OrderType.SERVICE,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goOrderSuccess: (orderId, type) => {
    NavigatorUtil.doDispatch({
      key: "OrderSuccess",
      type: "ReplaceTwoScreen",
      routeName: "OrderSuccess",
      params: {
        orderId,
        type,
      },
    });
  },
  goOrderInfo: (orderId, type) => {
    const action1 = NavigationActions.navigate({
      routeName: "OrderInfo",
      params: {
        orderId,
        type,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goRecycleOrderInfo: orderId => {
    const action1 = NavigationActions.navigate({
      routeName: "OrderInfo",
      params: {
        orderId,
        type: OrderType.RECYCLE,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goServiceOrderInfo: orderId => {
    const action1 = NavigationActions.navigate({
      routeName: "OrderInfo",
      params: {
        orderId,
        type: OrderType.SERVICE,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  goUpdateOrderInfo: orderId => {
    const action1 = NavigationActions.navigate({
      routeName: "OrderInfo",
      params: {
        orderId,
        type: OrderType.UPDATE,
      },
    });
    NavigatorUtil.doDispatch(action1);
  },
  lunchHome: param => {
    const action = NavigationActions.navigate({
      routeName: "HomeTab",
    });
    NavigatorUtil.doDispatch(action);
  },
  goHome: param => {
    NavigatorUtil.doDispatch({
      key: "goHome",
      type: "goHome",
      routeName: "HomeTab",
    });
  },
  bindMobileBack: param => {
    NavigatorUtil.doDispatch({
      key: "bindMobileBack",
      type: "backTwoPage",
      routeName: "Setting",
    });
  },
  goReAccess: param => {
    NavigatorUtil.doDispatch({
      key: "goReAccess",
      type: "backTwoPage",
      routeName: "RecyclePriceInfo",
    });
  },
  goNewAddress: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "EditAddress",
        params: {
          title: "添加收货地址",
          type: EditType.NEW,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goRecycleAccessResult: (deviceInfo, recycleValues) => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "RecycleAccessResult",
        params: {
          deviceInfo,
          recycleValues,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goPlaceRecycleOrder: recycleOrderPlan => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "PlaceRecycleOrder",
        params: {
          recycleOrderPlan,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goQuickOrder: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "QuickOrder",
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goAddressList: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "AddressList",
        params: {
          title: "地址管理",
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goAddressListCallBack: callback => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "AddressList",
        params: {
          title: "地址管理",
          callback,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goEditUserInfo: userInfo => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "EditUserInfo",
        params: { userInfo },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  BindMobileGetCode: type => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "BindMobileGetCode",
        params: { type },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goBindMobile: (mobile, type) => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "BindMobile",
        params: { mobile, type },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goEditAddress: address => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "EditAddress",
        params: {
          title: "编辑收货地址",
          type: EditType.EDIT,
          address,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goServiceOrderList: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "OrderList",
        params: {
          title: "维修订单",
          type: OrderType.SERVICE,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goUpdateOrderList: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "OrderList",
        params: {
          title: "升级订单",
          type: OrderType.UPDATE,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goRecycleOrderList: () => {
    NavigatorUtil.checkLogin().then(() => {
      const action1 = NavigationActions.navigate({
        routeName: "OrderList",
        params: {
          title: "回收订单",
          type: OrderType.RECYCLE,
        },
      });
      NavigatorUtil.doDispatch(action1);
    });
  },
  goStoreAddress: address => {
    NavigatorUtil.checkLogin().then(() => {
      Alert.alert("门店地址", address, [{ text: "确定", onPress: () => console.log("OK Pressed") }], {
        cancelable: false,
      });
    });
  },
  logOut: () => {
    StorageUtil.clearUser();
  },
  jumpTo: (routeName, params = {}) => {
    const action1 = NavigationActions.navigate({
      routeName,
      params,
    });
    NavigatorUtil.doDispatch(action1);
  },
};
export default NavigatorUtil;
